home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Info-Mac 4
/
Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso
/
Science
/
RLaB
/
rlib
/
diff.r
< prev
next >
Wrap
Text File
|
1994-04-25
|
814b
|
40 lines
//-------------------------------------------------------------------//
// Syntax: diff ( X )
// diff ( X , k )
// Description:
// The function diff computes the difference between adjacent
// elements in a vector. The function diff merely does:
// x = x[2:n] - x[1:n-1];
// If X is a matrix, then the operation is performed on each column
// of X.
// The default value for `k' is 1. If k is specified as other than 1,
// then the operation is performed k times.
//-------------------------------------------------------------------//
diff = function ( X, k )
{
local (i, m, n, x);
if (!exist (k)) { k = 1; }
x = X;
for (i in 1:k)
{
m = size(x)[1]; n = size(x)[2];
if (m == 1)
{
x = x[2:n] - x[1:n-1];
else
x = x[2:m;] - x[1:m-1;];
}
}
return x;
};